<?php
namespace app\index\controller;

use think\Db;
use think\Request;

class Index extends Base
{
    public function index()
    {
        return $this->fetch('index');
    }

    public function detail()
    {
        $request = Request::instance();
        $data = $request->post('data');
        list($codeNum,$code) = explode(',', $data);
        /**查询商品*/
        $ret = Db::table('market_goods')->where(array('code'=>$code))->field('name,price')->find();
        is_null($ret)?$this->apiResponse('200', '查询失败', '暂无该产品'):
            $this->apiResponse('200', '查询成功',  "商品名称：{$ret['name']}，商品价格：{$ret['price']}");
    }

    public function addGoods()
    {
        $request = Request::instance();
        $name = $request->post('name');
        $price = $request->post('price');
        $code = $request->post('code');
        $data = [
            'name'=>$name,
            'price'=>$price,
            'code'=>$code,
            'create_time'=>time(),
        ];
        $is_ret = Db::table('market_goods')->where(array('code'=>$code))->field('id')->find();
        $is_ret?$this->apiResponse('200', '商品已经存在'):true;
        $ret = Db::table('market_goods')->insert($data);
        $ret?$this->apiResponse('200', '插入成功'):
            $this->apiResponse('500', '插入失败');
    }
}
